import request from '@/utils/request'
import type { ApiResponse, BlogUser, LoginForm, RegisterForm } from '@/types/api'
import { mockData, mockDelay } from '@/mock'

// 是否使用mock数据
const USE_MOCK = true

// 用户注册
export async function register(data: RegisterForm): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return mockData.user.register
  }
  return request({
    url: '/blog/user/register',
    method: 'post',
    data
  })
}

// 用户登录
export async function login(data: LoginForm): Promise<ApiResponse<{ token: string; user: BlogUser }>> {
  if (USE_MOCK) {
    await mockDelay()
    return mockData.user.login
  }
  return request({
    url: '/blog/user/login',
    method: 'post',
    data
  })
}

// 获取用户信息
export async function getUserInfo(): Promise<ApiResponse<BlogUser>> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '获取成功',
      data: mockData.user.login.data.user
    }
  }
  return request({
    url: '/blog/user/profile',
    method: 'get'
  })
}

// 更新用户信息
export async function updateUserInfo(data: Partial<BlogUser>): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '更新成功',
      data: null
    }
  }
  return request({
    url: '/blog/user/profile',
    method: 'put',
    data
  })
}

// 修改密码
export async function updatePassword(data: { oldPassword: string; newPassword: string }): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '密码修改成功',
      data: null
    }
  }
  return request({
    url: '/blog/user/updatePwd',
    method: 'put',
    data
  })
}

// 检查用户名是否唯一
export async function checkUsernameUnique(username: string): Promise<ApiResponse<boolean>> {
  if (USE_MOCK) {
    await mockDelay(200)
    return {
      code: 200,
      msg: '检查完成',
      data: username !== 'admin'
    }
  }
  return request({
    url: '/blog/user/checkUsernameUnique',
    method: 'get',
    params: { username }
  })
}

// 检查邮箱是否唯一
export async function checkEmailUnique(email: string): Promise<ApiResponse<boolean>> {
  if (USE_MOCK) {
    await mockDelay(200)
    return {
      code: 200,
      msg: '检查完成',
      data: email !== 'admin@example.com'
    }
  }
  return request({
    url: '/blog/user/checkEmailUnique',
    method: 'get',
    params: { email }
  })
}

// 获取验证码
export async function getCaptcha(): Promise<ApiResponse<{ img: string; uuid: string }>> {
  if (USE_MOCK) {
    await mockDelay(300)
    return mockData.user.captcha
  }
  return request({
    url: '/captchaImage',
    method: 'get'
  })
}

// 发送邮箱验证码
export async function sendEmailCode(email: string): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay(1000)
    return {
      code: 200,
      msg: '验证码已发送',
      data: null
    }
  }
  return request({
    url: '/blog/user/sendEmailCode',
    method: 'post',
    data: { email }
  })
}

// 重置密码
export async function resetPassword(data: { email: string; code: string; newPassword: string }): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '密码重置成功',
      data: null
    }
  }
  return request({
    url: '/blog/user/resetPassword',
    method: 'post',
    data
  })
}

// 获取用户笔记
export async function getUserNotes(): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '获取成功',
      data: []
    }
  }
  return request({
    url: '/blog/user/notes',
    method: 'get'
  })
}

// 获取用户收藏
export async function getUserFavorites(): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '获取成功',
      data: []
    }
  }
  return request({
    url: '/blog/user/favorites',
    method: 'get'
  })
}

// 获取用户点赞
export async function getUserLikes(): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '获取成功',
      data: []
    }
  }
  return request({
    url: '/blog/user/likes',
    method: 'get'
  })
}

// 删除笔记
export async function deleteNote(noteId: number): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '删除成功',
      data: null
    }
  }
  return request({
    url: `/blog/notes/${noteId}`,
    method: 'delete'
  })
}

// 取消收藏
export async function removeFavorite(noteId: number): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '取消收藏成功',
      data: null
    }
  }
  return request({
    url: `/blog/user/favorites/${noteId}`,
    method: 'delete'
  })
}

// 取消点赞
export async function removeLike(noteId: number): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '取消点赞成功',
      data: null
    }
  }
  return request({
    url: `/blog/user/likes/${noteId}`,
    method: 'delete'
  })
}

// 注销账户
export async function deleteAccount(): Promise<ApiResponse> {
  if (USE_MOCK) {
    await mockDelay()
    return {
      code: 200,
      msg: '账户注销成功',
      data: null
    }
  }
  return request({
    url: '/blog/user/account',
    method: 'delete'
  })
}